package com.contory.ssn_lgnd.framework.gl;

import android.opengl.GLES20;

public class FrameBufferObject {
	int fboId = -1;
	public int width;
	public int height;
	public Texture_NoBitmap tex;
	public TextureRegion tr;
	boolean isTexReloadTime = false;
	
	public FrameBufferObject(int width, int height){
		tr = new TextureRegion(width, height, 0, 0, width, height);
		tr.flipCoordY();
		this.width = width; this.height = height;
	}
	
	public float getWidthPerFB(){
		return (float)1/width;
	}
	
	public float getHeightPerFB(){
		return (float)1/height;
	}
	
	public void load(){
		tex = new Texture_NoBitmap(width, height);
		int[] fboIds = new int[1];
		GLES20.glGenFramebuffers(1, fboIds, 0);
		fboId = fboIds[0];
		fboIds = null;
		GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, fboId);
		GLES20.glFramebufferTexture2D(GLES20.GL_FRAMEBUFFER,
				GLES20.GL_COLOR_ATTACHMENT0, GLES20.GL_TEXTURE_2D,
				tex.getTexId(), 0);
		GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, 0);
	}
	
	public void bind(){
		GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, fboId);
	}
	
	public void unBind(){
		GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, 0);
	}
	
	public TexturePtr getTexture(){
		return tex;
	}
	
	public TextureRegion getTextureRegion(){
		return tr;
	}
}
